<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #container {
        width: 500px;
        height: 500px;
        position: relative;
        overflow: hidden;
      }

      #image {
        width: 100%;
        height: 100%;
        transition: transform 0.2s;
        transform-origin: 0 0;
      }
    </style>
  </head>
  <body>
    <div id="container">
      <img src="./test.png" id="image" />
    </div>
  </body>
  <script>
    const container = document.getElementById("container");
    const image = document.getElementById("image");

    let factor = {
      scale: 1,
    };

    container.addEventListener("wheel", (event) => {
      event.preventDefault();

      const scaleDelta = event.deltaY * -1; // 可根据实际需求调整缩放速度
      const offsetX = event.offsetX / container.clientWidth;
      const offsetY = event.offsetY / container.clientHeight;

      //   const scale = Math.max(
      //     0.2,
      //     Math.min(image.clientWidth / container.clientWidth + scaleDelta, 3)
      //   ); // 设定缩放范围

      const scaleVal = scaleDelta > 0 ? 0.2 : -0.2;
      factor.scale += scaleVal;
      if (factor.scale < 1) {
        factor = {
          scale: 1,
        };
      }

      image.style.transformOrigin = `${offsetX * 100}% ${offsetY * 100}%`;
      image.style.transform = `scale(${factor.scale})`;
    });
  </script>
</html>
